<template></template>
<script lang="ts">
export default {
  name: "refreshtoken"
}
</script>
<script lang="ts" setup>
import { ref, watch, onMounted,onUnmounted } from "vue";
import { useUserStore } from "../../store/user";
import webosutils from 'webosutils';
import { useRoute } from "vue-router";
const useStore = useUserStore();
const route = useRoute();
const refrensh = ref(0);
const isTokenExpired = (expires: number) => {
  let curTime = new Date().getTime();
  let expiresTime = expires - curTime;
  //console.log("到期：" + expires);
  let expiresTime1 = (expiresTime - (1000 * 2 * 10));
  //console.log("剩余：" + expiresTime1);
  // 还差10分钟即将过期或者已经过期了，但过期时间在20分钟内
  if (expiresTime <= 0 || expiresTime1 <= 0) {
    return true
  }
  return false;
}
onMounted(() => {
 // setTimeout(() => {
    //  ;
    //console.log("开始计时");
    var document = window.document as any;
   // win.addEventListener('scroll', handleScroll);  
    //win.addEventListener('scroll', handleResize);  
  //   var body = document.querySelector('html');
  //   body.addEventListener("click", handleClick);
  //   body.addEventListener("scroll", handleScroll);
  //   body.addEventListener("keydown", handlekeydown);
  //  body.addEventListener("mousemove", handleMousemove);
  //  body.addEventListener("mousewheel", handleMousewheel);

  //}, 200);

});
onUnmounted(()=>{
  clearRefreshToken();
})
const handleScroll = () => {
 // console.log('页面被滚动了！');
  RefreshToken();
}
const handleMousemove = () => {
 // console.log('鼠标移动');
  RefreshToken();
}
const handleMousewheel = () => {
  //console.log('鼠标离开');
  RefreshToken();
}
const handleClick = () => {
 // console.log('点击了页面');
  RefreshToken();
}
const handlekeydown = () => {
  RefreshToken();
  //console.log('按下键盘');
}
const handleResize = () => {
  //console.log('窗口大小变化了！');
  RefreshToken();
}
//计时判断是否过期
const RefreshToken = () => {

  //while (1) 

  
  //   if (isTokenExpired(useStore.expires_in)) {
  //   webosutils.http.post("/v1/api/users/RefreshToken", {}).then((resp: any) => {
  //     if (resp.success) {
  //       useStore.expires_in = resp.data.expires_in;
  //       useStore.token = resp.data.token;
  //     }

  //   }).catch(() => {
  //     console.log("错误！");
  //   });
  // }

  refrensh.value = setInterval(() => {
    // console.log("开始计时");
    if (isTokenExpired(useStore.expires_in)) {
      webosutils.http.post("/v1/api/users/RefreshToken", {}).then((resp: any) => {
        if (resp.success) {
           
          useStore.expires_in = resp.data.expires_in;
          useStore.token = resp.data.token;
          
        }

      }).catch(() => {
        //console.log("错误！");
      });
    }
  }, 5000);
}
//清除计时器
const clearRefreshToken = () => {
  if (refrensh.value != 0) {
   // console.log("已清除计时器");
    clearInterval(refrensh.value);
  }



}
//开始计时
RefreshToken();
//当路由为login清除计时器
// watch(route, () => {
//   //登录是清除计时器
//   if (route.fullPath.indexOf("login") > -1) {
//     console.log("已清除计时器");
//     clearRefreshToken();
//   };

// })
</script>